# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request, FormRequest
import re
import datetime
from logincsdn.items import LogincsdnItem
import time

class LoginSpider(scrapy.Spider):
	name = 'login'
	allowed_domains = ['csdn.net']
	header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
	#start_urls = ['http://csdn.net/']
	def start_requests(self):
		return [Request('https://passport.csdn.net/account/verify', headers=self.header, meta={'cookiejar': 1}, callback=self.parse)]

	def parse(self, response):
		#判断是否有验证码

		lt = response.xpath('//input[@name="lt"]/@value').extract()
		execution = response.xpath('//input[@name="execution"]/@value').extract()
		eventId = response.xpath('//input[@name="_eventId"]/@value').extract()

		#POST的数据
		data = {
			'username': '**********',
			'password': '**********',
			'lt': lt,
			'execution': execution,
			'eventId': eventId
		}
		
		print('正在登录。。。')
		#通过FormRequest.from_response()进行登录
		return [FormRequest.from_response(response,
			meta={'cookiejar': response.meta['cookiejar']},
			headers=self.header,
			formdata=data,
			callback=self.next
			)]

	def next(self, response):
		print(response.body.decode('utf-8'))
		for i in range(1, 245):
			yield Request('https://edu.csdn.net/courses/k/p'+str(i), headers=self.header, meta={'cookiejar': response.meta['cookiejar']}, callback=self.list1)
			time.sleep(1)
		for j in range(1, 21):
			yield Request('https://edu.csdn.net/courses/l0_t/p'+str(j), headers=self.header, meta={'cookiejar': response.meta['cookiejar']}, callback=self.list2)
			time.sleep(1)

	def list1(self, response):
		link = response.xpath('//div[@class="course_dl_list"]/a/@href').extract()

		for i in link:
			yield Request(i, headers=self.header, meta={'cookiejar': response.meta['cookiejar']}, callback=self.result)

	def result(self, response):
		title = response.xpath('//a[@class="titleimg"]/img/@alt').extract()
		num = response.xpath('//span[@class="num"]/text()').extract()
		money = response.xpath('//span[@class="money"]/text()').extract()

		pinfo = response.xpath('//span[@class="pinfo"]/text()').extract()
		
		item = LogincsdnItem()
		item['title'] = re.sub('\s', '', title[0])
		item['num'] = re.sub('\s', '', num[0])
		item['money'] = re.sub('\s', '', money[0])
		item['pinfo'] = re.sub('\s', '', pinfo[0])
		item['coursenum'] = ''

		yield item

	def list2(self, response):
		link = response.xpath('//div[@class="course_dl_list"]/a/@href').extract()
		for i in link:
			yield Request(i, headers=self.header, meta={'cookiejar': response.meta['cookiejar']}, callback=self.result2)

	def result2(self, response):
		title = response.xpath('//div[@class="pull-right cont"]/h3/@title').extract()
		coursenum = response.xpath('//span[@data-mod="popu_446"]/dl[1]/dd/text()').extract()
		money = response.xpath('//span[@data-mod="popu_446"]/dl[3]/dd[@class="now_price"]/text()').extract()
		pinfo = response.xpath('//span[@data-mod="popu_446"]/dl[2]/dd/text()').extract()

		item = LogincsdnItem()
		item['title'] = re.sub('\s', '', title[0])
		item['coursenum'] = re.sub('\s', '', coursenum[0])
		item['money'] = re.sub('\s', '', money[0])
		item['pinfo'] = re.sub('\s', '', pinfo[0])
		item['num'] = ''

		yield item